package fr.teuteuf.runstickmanrun.screen;

import tv.ouya.console.api.OuyaController;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;

import fr.teuteuf.runstickmanrun.Constants;
import fr.teuteuf.runstickmanrun.RunStickmanRun;
import fr.teuteuf.runstickmanrun.entity.Player;

public class MultiplayerRankScreen extends AbstractScreen {
	
	private final int NB_MENU_ITEMS = 2;
	
	private OrthographicCamera interfaceCamera;
	private SpriteBatch interfaceBatch;
	
	private Array<Player> rankingPlayers;
	private int menuPicker;
	private float totalTimeElapsed;
	
	public MultiplayerRankScreen(RunStickmanRun runStickmanRun) {
		this.runStickmanRun = runStickmanRun;
	}

	@Override
	public void create() {
		super.create();
		reinit();
	}

	@Override
	public void reinit() {
		menuPicker = -1;
		totalTimeElapsed = 0;
		rankingPlayers = ((MultiPlayerScreen) runStickmanRun.getScreen(RunStickmanRun.INDEX_MULTIPLAYER_SCREEN)).getInvertedRanking();
		rankingPlayers.reverse();
		interfaceCamera = new OrthographicCamera();
		interfaceCamera.setToOrtho(false, Constants.WIDTH_RESOLUTION, Constants.HEIGHT_RESOLUTION);
		interfaceBatch = new SpriteBatch();
	}

	@Override
	public void update(float elapsedTime) {
		interfaceCamera.update();
		totalTimeElapsed += elapsedTime;
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		interfaceBatch.setProjectionMatrix(interfaceCamera.combined);
		interfaceBatch.begin();
		
		defaultFont.draw(interfaceBatch, "Run, Stickman, Run!", Constants.WIDTH_RESOLUTION/2 - 100, Constants.HEIGHT_RESOLUTION - 100);
		defaultFont.draw(interfaceBatch, "Ranking:", Constants.WIDTH_RESOLUTION/2 - 75, Constants.HEIGHT_RESOLUTION - 200);
		for(int i = 0; i < rankingPlayers.size; i++) {
			defaultFont.draw(interfaceBatch, (i+1)+" - "+rankingPlayers.get(i).getName(), Constants.WIDTH_RESOLUTION/2 - 100, Constants.HEIGHT_RESOLUTION - 250 - 20 * i);
		}
		if((int)(totalTimeElapsed * 5) % 2 == 0) {
			if(menuPicker > -1) {
				defaultFont.draw(interfaceBatch, ">>", Constants.WIDTH_RESOLUTION/2 - 150, 200 - menuPicker * 30);
			} else {
				defaultFont.draw(interfaceBatch, "<<", Constants.WIDTH_RESOLUTION/2 - 150, 200 - menuPicker * 30);
				defaultFont.draw(interfaceBatch, ">>", Constants.WIDTH_RESOLUTION/2 + 75, 200 - menuPicker * 30);
			}
		}
		defaultFont.draw(interfaceBatch, "DIFFICULTY = "+runStickmanRun.getSelectedLevel(), Constants.WIDTH_RESOLUTION/2 - 90, 230);
		defaultFont.draw(interfaceBatch, "[RETRY]", Constants.WIDTH_RESOLUTION/2 - 70, 200);
		defaultFont.draw(interfaceBatch, "[HOME SCREEN]", Constants.WIDTH_RESOLUTION/2 - 95, 170);
		defaultFont.draw(interfaceBatch, "User: "+runStickmanRun.getUsername(), 20, 20);
		
		interfaceBatch.end();
	}

	@Override
	public void dispose() {
	}

	@Override
	public boolean keyDown(int keycode) {
		if (runStickmanRun.isOnOuya() && keycode == OuyaController.BUTTON_DPAD_UP
				|| ! runStickmanRun.isOnOuya() && keycode == Keys.UP) {
			if (menuPicker > -1) {
				menuPicker = (menuPicker - 1) % NB_MENU_ITEMS;
			}
		} else if (runStickmanRun.isOnOuya() && keycode == OuyaController.BUTTON_DPAD_DOWN 
				|| ! runStickmanRun.isOnOuya() && keycode == Keys.DOWN) {
			if (menuPicker < NB_MENU_ITEMS - 1) {
				menuPicker = (menuPicker + 1) % NB_MENU_ITEMS;
			}
		} else if (runStickmanRun.isOnOuya()&& keycode == OuyaController.BUTTON_O
				|| ! runStickmanRun.isOnOuya() && keycode == Keys.ENTER) {
			if (menuPicker == 0) {
				runStickmanRun.changeCurrentScreen(RunStickmanRun.INDEX_MULTIPLAYER_SCREEN, true);
			} else if (menuPicker == 1) {
				runStickmanRun.changeCurrentScreen(RunStickmanRun.INDEX_HOME_SCREEN, true);
			}
		} else if(runStickmanRun.isOnOuya() && keycode == OuyaController.BUTTON_DPAD_LEFT
		  ||    ! runStickmanRun.isOnOuya() && keycode == Keys.LEFT) {
			if(menuPicker == -1 && runStickmanRun.getSelectedLevel() > 0) {
				runStickmanRun.setSelectedLevel(runStickmanRun.getSelectedLevel() - 1);
			}
		} else if(runStickmanRun.isOnOuya() && keycode == OuyaController.BUTTON_DPAD_RIGHT
		  ||    ! runStickmanRun.isOnOuya() && keycode == Keys.RIGHT) {
			if(menuPicker == -1 && runStickmanRun.getSelectedLevel() < Constants.DIFFICULTY_MAX) {
				runStickmanRun.setSelectedLevel(runStickmanRun.getSelectedLevel() + 1);
			}
		}
		
		return true;
	}

	@Override
	public boolean buttonDown(Controller arg0, int arg1) {
		keyDown(arg1);
		return true;
	}
}
